package com.jyh.service.channel;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jyh.config.BaseAction;
import com.jyh.config.datasource.TargetDataSource;
import com.jyh.config.quantity.Result;
import com.jyh.db.entity.JyhChannelList;
import com.jyh.db.entity.JyhChannelListExample;
import com.jyh.db.mapper.JyhChannelListMapper;
import com.jyh.util.Utils;

@Service
public class ChannelService extends BaseAction {
	@Autowired
	private JyhChannelListMapper channelMapper;

	/**
	 * 获取渠道一级列表
	 * 
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "user")
	public List<JyhChannelList> channelSelect(HttpServletRequest request, HttpSession session) {
		JyhChannelListExample example = new JyhChannelListExample();
		example.createCriteria().andGeneralizeIdLike("____");
		List<JyhChannelList> list = channelMapper.selectByExample(example);

		return list;
	}

	/**
	 * 查询下级渠道
	 * 
	 * @param key
	 * @return
	 */
	@TargetDataSource(name = "user")
	public List<JyhChannelList> selectLower(String key) {
		JyhChannelListExample example = new JyhChannelListExample();
		example.createCriteria().andGeneralizeIdLike(key + "____");
		List<JyhChannelList> list = channelMapper.selectByExample(example);
		return list;
	}
	@TargetDataSource(name = "user")
	public List<JyhChannelList> selectAbb(String key) {
		JyhChannelListExample example = new JyhChannelListExample();
		example.createCriteria().andGeneralizeIdLike(key);
		List<JyhChannelList> list = channelMapper.selectByExample(example);
		return list;
	}
	@TargetDataSource(name = "user")
	public String getAdd(JyhChannelList vo) {
		String gId = vo.getGeneralizeId();
		String generalizeName = vo.getChannelname();
		String downloadLink = vo.getChannelnumber();
		if (Utils.isNull(generalizeName) || Utils.isNull(downloadLink)) {
			return "请填写完整信息";
		}
		// 查询使用
		String channel = "";
		// 拼接入库使用
		String channelId = "";
		if ("无".equals(gId)) {
			channel = "____";
		} else if (gId.length() == 20) {
			channel = gId.substring(0, gId.length() - 4) + "____";
			channelId = gId.substring(0, gId.length() - 4);
		} else {
			channel = gId + "____";
			channelId = gId;
		}

		JyhChannelListExample example = new JyhChannelListExample();
		example.createCriteria().andGeneralizeIdLike(channel);
		List<JyhChannelList> list = channelMapper.selectByExample(example);
		String generalize_id = "";
		if (list != null && list.size() != 0) {
			generalize_id = list.get(list.size() - 1).getGeneralizeId();
		}

		if (generalize_id.length() > 3) {
			int ids = Integer
					.parseInt(generalize_id.substring(generalize_id.length() - 4, generalize_id.length()) + "");
			if (ids < 9) {
				String idcord = channelId + "000" + (ids + 1);
				vo.setGeneralizeId(idcord);
			} else if (ids >= 9 && ids < 99) {
				String idcord = channelId + "00" + (ids + 1);
				vo.setGeneralizeId(idcord);
			} else if (ids >= 99 && ids < 999) {
				String idcord = channelId + "0" + (ids + 1);
				vo.setGeneralizeId(idcord);
			} else {
				String idcord = channelId + "" + (ids + 1);
				vo.setGeneralizeId(idcord);
			}

		} else {
			if ("".equals(generalize_id)) {
				vo.setGeneralizeId(channelId + "0001");
			} else {
				int ids = Integer.parseInt(generalize_id);
				if (ids < 10) {
					vo.setGeneralizeId("000" + (ids + 1));
				} else if (ids > 10 && ids < 100) {
					vo.setGeneralizeId("00" + (ids + 1));
				} else if (ids > 100 && ids < 1000) {
					vo.setGeneralizeId("0" + (ids + 1));
				} else {
					vo.setGeneralizeId((ids + 1) + "");
				}
			}
		}
		int insertInt = channelMapper.insertSelective(vo);
		if (insertInt <= 0) {
			return "插入失败";
		}
//		JyhChannelListExample channelExample = new JyhChannelListExample();
//		channelExample.createCriteria().andGeneralizeIdEqualTo(vo.getGeneralizeId()).andChannelnameEqualTo(vo.getChannelname());
//		List<JyhChannelList> listVo = channelMapper.selectByExample(channelExample);
//		if (listVo == null || listVo.size() == 0 || listVo.size() > 2) {
//			return "数据插入异常请及时与开发联系并截图保留，如果忽略将会数据异常！！！";
//		}
//		int autoId = listVo.get(0).getId();
//		JyhChannelList vos = new JyhChannelList();
//		vos.setId(autoId);
//		vos.setLinkSite(Utils.codingEncrypt(autoId + ""));
//		channelMapper.updateByPrimaryKeySelective(vos);
		return "插入成功";
	}
	@TargetDataSource(name = "user")
	public String addHtml(String url, JyhChannelList vo, String button) {
		String htmlStr = "<form action=\"" + url + "\"> <input type=\"hidden\" id=\"id\" name =\"id\" value=\""
				+ Utils.getStrForSkys(vo.getId() + "") + "\" /> "
				+ "<input type=\"hidden\" id=\"generalizeId\" name = \"generalizeId\" value=\""
				+ Utils.getStrForSky(vo.getGeneralizeId()) + "\" /> "
				+ "<input type=\"hidden\" id=\"linkSite\" name = \"linkSite\" value=\""
				+ Utils.getStrForSky(vo.getLinkSite()) + "\" /> "
				+ "<table><tr align=\"center\"><td align=\"right\"><span class=\"redtxt\">添加渠道名称： </span> "
				+ "<input type=\"text\" name=\"channelname\" id=\"channelname\" value=\""
				+ Utils.getStrForSky(vo.getChannelname()) + "\">"
				+ "</td> <td align=\"right\"><span class=\"redtxt\">添加渠道简称： </span>"
				+ "<input type=\"text\" name=\"generalizeAbbreviation\" id=\"generalizeAbbreviation\" value=\""
				+ Utils.getStrForSky(vo.getGeneralizeAbbreviation()) + "\">"
				+ "</td> <td align=\"right\"><span class=\"redtxt\">添加下载链接： </span>"
				+ "<input type=\"text\" name=\"channelnumber\" id=\"channelnumber\" value=\""
				+ Utils.getStrForSky(vo.getChannelnumber()) + "\">"
				+ "</td><td align=\"right\"><input type=\"submit\" value=\"" + button + "\"></td><tr></table></form>";
		;
		return htmlStr;
	}
	@TargetDataSource(name = "user")
	public Result selectUpdate(String url, String key) {
		if (Utils.isNull(key)) {
			return new Result("9999", "请选择修改项后再次点击修改！");
		}
		JyhChannelListExample example = new JyhChannelListExample();
		example.createCriteria().andGeneralizeIdEqualTo(key);
		List<JyhChannelList> list = channelMapper.selectByExample(example);
		if (list == null || list.size() == 0 || list.size() >= 2) {
			return new Result("9999", "数据插入异常请及时与开发联系并截图保留，如果忽略将会数据异常！！！");
		}
		JyhChannelList vo = list.get(0);
		url = url + "/channel/update/";
		String htmlStr = addHtml(url, vo, "修改");

		// TODO Auto-generated method stub
		return new Result("0000", htmlStr);
	}
	@TargetDataSource(name = "user")
	public String getUpdate(JyhChannelList vo) {
		vo.setLinkSite(null);
		int insertInt = channelMapper.updateByPrimaryKeySelective(vo);
		if (insertInt > 0) {
			return "修改渠道成功";
		} else {
			return "修改失败";
		}
	}

}
